package com.hyfrogx.modules.information.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.modules.app.entity.User;
import com.hyfrogx.modules.app.service.UserService;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.information.dao.InfoCommentDao;
import com.hyfrogx.modules.information.entity.InfoCommentEntity;
import com.hyfrogx.modules.information.service.InfoCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liubin
 * @date 2019/11/1 10:41
 */
@Service("InfoCommentService")
@Slf4j
public class InfoCommentServiceImpl extends ServiceImpl<InfoCommentDao, InfoCommentEntity>
  implements InfoCommentService {
  @Autowired
  private UserService userService;


  @Override
  public List<InfoCommentEntity> getInformationCommentforApi(Integer infoId) {
    return this.baseMapper.getInformationCommentforApi(infoId);
  }

  @Override
  public List<InfoCommentEntity> getCommentReplyforApi(Integer id) {
    return this.baseMapper.getCommentReplyforApi(id);
  }

  @Override
  public Page getAllReplyComments(QueryExt query) {
    Map map = new HashMap(16);
    map.putAll(query.getCondition());
    List<InfoCommentEntity> result = this.baseMapper.getAllReplyComments(query, map);
    query.setRecords(result);
    return query;
  }

  @Override
  public Page getReplyCommentsByUid(QueryExt query) {
    Map map = new HashMap(16);
    map.putAll(query.getCondition());
    List<InfoCommentEntity> result = this.baseMapper.getReplyCommentsByUid(query, map);
    query.setRecords(result);
    return query;
  }

  @Override
  public Page getInformationComment(QueryExt query) {
    Map map = new HashMap(16);
    map.putAll(query.getCondition());
    List<InfoCommentEntity> result = this.baseMapper.getInformationCommentforApi(Integer.parseInt(map.get("infoId").toString()));
    if (CollUtil.isNotEmpty(result)) {
      // 解析评论对象所有回复
      List<InfoCommentEntity> listhuifu;
      Integer count = result.size();
      for (int i = 0; i < result.size(); i++) {
        listhuifu = this.baseMapper.getCommentReplyforApi(result.get(i).getId());
        if (listhuifu != null) {
          result.get(i).setInList(listhuifu);
          for (int j = 0; j < listhuifu.size(); j++) {
            User user = userService.getUserSelfInfoByUid(listhuifu.get(j).getUid());
            if (user != null) {
              listhuifu.get(j).setUser(user);
            }
          }
        }
        User user = userService.getUserSelfInfoByUid(result.get(i).getUid());
        if (user != null) {
          result.get(i).setUser(user);
        } else {
          this.baseMapper.deleteById(result.get(i).getId());
        }
      }
    }
    query.setRecords(result);
    return query;
  }

  @Override
  public Page getOldInformationComments(QueryExt queryExt) {
    Map map = new HashMap(16);
    map.putAll(queryExt.getCondition());
    List<InfoCommentEntity> result = this.baseMapper.getOldInformationComments(queryExt, map);
    queryExt.setRecords(result);
    return queryExt;
  }

  @Override
  public Page getAllInformationComments(QueryExt queryExt) {
    Map map = new HashMap(16);
    map.putAll(queryExt.getCondition());
    List<InfoCommentEntity> result =  this.baseMapper.getAllInformationComment(queryExt, map);
    queryExt.setRecords(result);
    return queryExt;
  }


  @Override
  public int replyCommet(InfoCommentEntity infoComment, Integer replyuid) {
    // 设置用户类型为管理员
    infoComment.setState(FrogConstant.BasicStatus.TRUE.getValue());
    infoComment.setUserType(0);
    infoComment.setPid(infoComment.getId());
    infoComment.setToUid(infoComment.getUid());
    infoComment.setUid(replyuid);
    infoComment.setCommentTime(new Date());
    infoComment.setId(null);
    // 新增评论默认展示状态为0展示 1不展示
    if (this.getBaseMapper().insert(infoComment) > 0) {
      return 1;
    } else {
      return -1;
    }
  }

  @Override
  public Integer commectInformation(InfoCommentEntity infoComment) {
/*    User user = userMapper.findUserByUid(infoComment.getUid());
    if (user.getRid() == 1) {
      // 设置用户类型为管理员
      infoComment.setUserType(0);
    } else {
      infoComment.setUserType(1);
    }*/

    infoComment.setUserType(1);
    infoComment.setCommentTime(new Date());
    // 新增评论默认展示状态为0展示 1不展示
    infoComment.setState(FrogConstant.INFOCOMMENTSTATUS.PENDING.getValue());
    infoComment.setPid(0);
    // 设置评论类型为0评论
    return this.baseMapper.insert(infoComment);
  }
}
